package com.qys.controller.history;

import com.qys.common.utils.RSAUtil;
import com.qys.controller.entiry.FileMsg;
import com.qys.controller.entiry.FinalURL;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fangc on 2019-10-22 23:03
 */
@Controller
public class HistoryListController {
    private final Logger logger = Logger.getLogger(HistoryListController.class);
    @RequestMapping(path = "/file/history")
    private ModelAndView show(ModelAndView mv){
        //通过HttpClient的get形式去获取最近10条记录
        JSONArray array = new JSONArray();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = null;
        HttpResponse httpResponse = null;
        JSONObject result = new JSONObject();
        try {
            httpGet = new HttpGet(FinalURL.HISTORY_URL);
            JSONObject object = RSAUtil.getReqHeard();
            httpGet.setHeader("X-SID", object.optString("data"));
            httpGet.setHeader("X-Signature", object.optString("sign"));
            httpGet.setHeader("publicKey", object.optString("publicKey"));
            httpResponse = httpClient.execute(httpGet);
            int status = httpResponse.getStatusLine().getStatusCode();
            System.out.println(httpResponse.getStatusLine());
            result.put("status", status);
            if (status == 200) {
                HttpEntity entity = httpResponse.getEntity();
                array = JSONArray.fromObject(EntityUtils.toString(entity));
            }
        } catch (Exception e) {
            logger.error("查询10条数据：" + e.getMessage());
        } finally {
            try {
                if (httpGet != null)
                    httpGet.clone();
            } catch (Exception e) {
                logger.error("关闭资源：" + e.getMessage());
            }
        }
        //将jsonArray转成list利于前端使用thymeleaf
        List<FileMsg> msgList = new ArrayList<>();
        JSONObject json = null;
        for (int i = 0 ; i < array.size() ; i++){
            json = array.getJSONObject(i);
            FileMsg fs = new FileMsg();
            fs.setOriginal(json.optString("original"));
            String path = json.optString("path");
            fs.setFileName(path.substring(path.lastIndexOf("\\") + 1));
            fs.setSize(json.optString("size"));
            fs.setType(json.optString("type"));
            String time = json.optString("createdata");
            fs.setCreatedata(time.substring(0 , time.length() -2));
            msgList.add(fs);
        }
        mv.setViewName("history");
        mv.addObject("Infos" , msgList);
        return mv;
    }
}
